﻿using System;
using System.Configuration;
using System.Linq;
using BusinessLogic.Model;
using BusinessLogic.StateManagers;
using BusinessLogic;

namespace Admin
{
    public partial class SettingsManagement : System.Web.UI.Page
    {
        private BlogNETDataContext dc;
        public BlogNETDataContext DataContext
        {
            get { return dc; }
        }

        #region PageEvents

        protected void Page_PreInit(object sender, EventArgs e)
        {
            dc = new BlogNETDataContext(ConfigurationManager.ConnectionStrings["BlogNETDB"].ConnectionString);
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                txtTitle.Text = (string)Application[Constants.Application.WebsiteTitle] ?? "";
                txtSlogan.Text = (string)Application[Constants.Application.WebsiteSlogan] ?? "";
                txtAbout.Text = (string)Application[Constants.Application.AboutContent] ?? "";
                string pageSize = ApplicationManager.CommentsPageSize;
                ddlCommentsPageSize.SelectedValue = String.IsNullOrEmpty(pageSize) ? SettingsDefaults.CommentsPageSize.ToString() : pageSize;
            }
        }

        protected void Page_Unload(object sender, EventArgs e)
        {
            if (dc != null)
            {
                dc.Dispose();
            }
        }

        #endregion

        #region ControlEvents

        protected void btnAddQuote_Click(object sender, EventArgs e)
        {
            if (txtQuote.Text.Trim().Length <= 255)
            {
                Quote quote = new Quote { Message = txtQuote.Text.Trim(), Author = txtAuthor.Text.Trim() };

                dc.Quotes.InsertOnSubmit(quote);
                dc.SubmitChanges();

                Cache.Remove(Constants.Cache.Quotes);
                Response.Redirect(Request.Url.ToString());
            }
            else
            {
                lblError.Visible = true;
                lblError.Text = "Quote text is longer than 255 characters.";
            }
        }

        protected void btnSaveSetting_Click(object sender, EventArgs e)
        {
            #region SetTitle

            string title = txtTitle.Text.Trim();

            if (((string)Application[Constants.Application.WebsiteTitle] ?? "") != title)
            {
                var currentTitle = (from t in dc.Settings
                                    where t.Key == Constants.Application.WebsiteTitle
                                    select t).SingleOrDefault();

                if (currentTitle != null)
                {
                    currentTitle.Value = title;
                }
                else
                {
                    Setting t = new Setting { Key = Constants.Application.WebsiteTitle, Value = title };
                    dc.Settings.InsertOnSubmit(t);
                }

                dc.SubmitChanges();
                Application[Constants.Application.WebsiteTitle] = title;
            }

            #endregion

            #region SetSlogan

            string slogan = txtSlogan.Text.Trim();

            if (((string)Application[Constants.Application.WebsiteSlogan] ?? "") != slogan)
            {
                var currentSlogan = (from s in dc.Settings
                                     where s.Key == Constants.Application.WebsiteSlogan
                                     select s).SingleOrDefault();

                if (currentSlogan != null)
                {
                    currentSlogan.Value = slogan;
                }
                else
                {
                    Setting s = new Setting { Key = Constants.Application.WebsiteSlogan, Value = slogan };
                    dc.Settings.InsertOnSubmit(s);
                }

                dc.SubmitChanges();
                Application[Constants.Application.WebsiteSlogan] = slogan;
            }

            #endregion

            #region SetAbout

            string about = txtAbout.Text.Trim();

            if (((string)Application[Constants.Application.AboutContent] ?? "") != about)
            {
                var currentAbout = (from a in dc.Settings
                                    where a.Key == Constants.Application.AboutContent
                                    select a).SingleOrDefault();

                if (currentAbout != null)
                {
                    currentAbout.Value = about;
                }
                else
                {
                    Setting a = new Setting { Key = Constants.Application.AboutContent, Value = about };
                    dc.Settings.InsertOnSubmit(a);
                }

                dc.SubmitChanges();
                Application[Constants.Application.AboutContent] = about;
            }

            #endregion

            #region SetCommentsPageSize

            string commentsPageSize = ddlCommentsPageSize.SelectedValue;

            if ((ApplicationManager.CommentsPageSize ?? "") != commentsPageSize)
            {
                var currentCommentsPageSize = (from c in dc.Settings
                                               where c.Key == "CommentsPageSize"
                                               select c).SingleOrDefault();

                if (currentCommentsPageSize != null)
                {
                    currentCommentsPageSize.Value = commentsPageSize;
                }
                else
                {
                    Setting c = new Setting { Key = "CommentsPageSize", Value = commentsPageSize };
                    dc.Settings.InsertOnSubmit(c);
                }

                dc.SubmitChanges();
                ApplicationManager.CommentsPageSize = commentsPageSize;
                CacheManager.RemoveLatestComments();
            }

            #endregion

            lbSaveSuccessful.Visible = true;
        }

        protected void gvQuotes_RowDeleted(object sender, System.Web.UI.WebControls.GridViewDeletedEventArgs e)
        {
            Cache.Remove(Constants.Cache.Quotes);
        }

        #endregion

    }
}